/**
 *
 */
package vn.tecviets.thuandt.controller.action.post;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vn.tecviets.thuandt.model.bean.Category;
import vn.tecviets.thuandt.model.bean.Post;
import vn.tecviets.thuandt.model.bo.CategoryBO;
import vn.tecviets.thuandt.model.bo.PostBO;
import vn.tecviets.thuandt.model.common.HTMLFile;
import vn.tecviets.thuandt.model.form.LoginActionForm;
import vn.tecviets.thuandt.model.form.PostActionForm;

/*
 * UpdatePostAction
 * 1.0
 * Mar 19, 2013
 * 
 * Copyright notice
 * 
 * Modification Logs:
 * 
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 19, 2013			Thuandt				Create
 */

public class UpdatePostAction extends Action {

	PostBO postBO = new PostBO();
	HTMLFile htmlFile = new HTMLFile();
	CategoryBO categoryBO = new CategoryBO();

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		/*
		 * Check login session
		 */
		LoginActionForm loginActionForm = (LoginActionForm) request.getSession().getAttribute("loginActionForm");
		if (loginActionForm == null) {
			return mapping.findForward("login");
		}
		
		PostActionForm postActionForm = (PostActionForm) form;

		try {
			
			int postId = Integer.parseInt(request.getParameter("postId"));
			Post post = postBO.getPostByID(postId);
			ArrayList<Category> categoryArrayList = categoryBO.getAllCategory();
			/*
			 * Check whether it is the first time the page is loaded
			 */
			if (request.getParameter("submit") == null) {

				postActionForm.setTitle(post.getTitle());
				postActionForm.setCategoryTitle(post.getCategoryTitle());
				postActionForm.setImage(post.getImage());
				postActionForm.setCaption(post.getCaption());
				postActionForm.setSummary(post.getSummary());
				postActionForm.setShowed(post.getShowed());
				postActionForm.setImportant(post.getImportant());
				postActionForm.setPathname(post.getPathname());
				postActionForm.setCategoryId(post.getCategoryId());
				postActionForm.setContent(htmlFile.readFile(post.getPathname()));
				
				request.setAttribute("categoryArrayList", categoryArrayList);
				
				return mapping.findForward("update");
			}
			
			/*
			 * Get all update info of a post
			 */
			String title = postActionForm.getTitle();
			int categoryId = postActionForm.getCategoryId();
			String image = postActionForm.getImage();
			String caption = postActionForm.getCaption();
			String summary = postActionForm.getSummary();
			String showed = postActionForm.getShowed();
			String important = postActionForm.getImportant();
			String content = postActionForm.getContent();
			
			/*
			 * Check whether some require info of a new post are inputted or not
			 */
			ActionErrors actionErrors = postBO.validateUpdate(image, caption, summary, content);
			saveErrors(request, actionErrors);
			int actionErrorsSize = actionErrors.size();
			if (actionErrorsSize != 0) {
				return mapping.findForward("update");
			}
			
			/*
			 * Update content and info of specific post
			 */
			htmlFile.updateFile(content, post.getPathname());
			postBO.updatePost(postId, title, categoryId, image, caption, summary, showed, important);
		
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw e;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

		return mapping.findForward("posts");
	}
}
